package xyz.xxdl.Service.Impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import xyz.xxdl.Service.AsdxApi;

import java.text.DecimalFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import static xyz.xxdl.utils.BtmxUtils.*;

@Component("AsdxApi")
public class AsdxApiImpl implements AsdxApi {
    static String ROUTE_PREFIX = "api/pro/v1";
    static String ROUTE_PREFIX_V2 = "api/pro/v2";
    private String host="https://asdx.io";  // https
    private String wss="wss://bitmax.io:443";
    public  static String uid;
    public  static String group;
    public  static String apikey;
    public  static String secret;





    @Override
    public  JSONObject query_prv_account_info() {
        Long ts = utc_timestamp();
        Map<String, String> headers = make_auth_headers(ts, "info", apikey, secret);
        String url = host+"/" + ROUTE_PREFIX + "/info";
        return sendHttpRequest(url, headers);
    }


// 查询余额
    @Override
    public JSONObject query_prv_balance(String asset, String account, String show_all) {

        Long ts = utc_timestamp();
        Map<String, String> headers  = make_auth_headers(ts, "balance", apikey, secret);
        String url =host+"/"+group+"/"+ROUTE_PREFIX+"/"+account+"/balance";
        // 设置请求参数
        HashMap<String, String> params = new HashMap<>();
        if(!"none".equals(asset)){
            params.put("asset",asset);
        }
        params.put("showAll",show_all);

        return doGet(url, headers, params);
    }



    public Double marketSell(String symbol){
        Double price = -1.0;
        try {
            String url = host+"/"+ROUTE_PREFIX+"/ticker";
            // 设置请求参数
            HashMap<String, String> params = new HashMap<>();
            params.put("symbol",symbol);
            JSONObject jsonObject = doGet(url, null, params);
            price = jsonObject.getJSONObject("data").getJSONArray("ask").getDouble(0);
        }catch (Exception e){
            System.out.println("卖一价格出错");
        }

        return price;
    }
    public Double marketBuy(String symbol){
        Double price = -1.0;
        try {
            String url = host+"/"+ROUTE_PREFIX+"/ticker";
            // 设置请求参数
            HashMap<String, String> params = new HashMap<>();
            params.put("symbol",symbol);
            JSONObject jsonObject = doGet(url, null, params);
            price = jsonObject.getJSONObject("data").getJSONArray("bid").getDouble(0);

        }catch (Exception e){
            System.out.println("买一价格出错");
        }

        return price;
    }


    public JSONObject order_new(String symbol, String price, String onceQty, String side) {
        String url = host+"/"+group+"/api/pro/v1/cash/order";
        Long ts = utc_timestamp();
        // 设置请求参数
        HashMap<String, Object> params = new HashMap<>();
//        params.put("id", UUID.randomUUID().toString());
        params.put("id", "");
        params.put("time",ts);
        params.put("orderPrice",price);
        params.put("orderQty",onceQty);
        params.put("orderType","limit");
        params.put("side",side);
        params.put("timeInForce","FOK");
        params.put("symbol",symbol);
        params.put("respInst","ACCEPT");
        Map<String, String> headers = make_auth_headers(ts, "order", apikey, secret);
        return doPost("POST", url, headers, params);
    }

    @Override
    public String orderNew(String symbol, String price, String onceQty, String side) {
        String status = "错误";
        try {
            JSONObject order = order_new(symbol, price, onceQty, side);
            String orderId = order.getJSONObject("data").getJSONObject("info").getString("orderId");
            JSONObject query = order_query(orderId);
            status= query.getJSONObject("data").getString("status");
        }catch (Exception e){
            e.printStackTrace();
        }

        return status;
    }


    @Override
    public JSONObject order_cancel(String account, String order_id, String symbol, String resp_inst) {
        // TODO python里面是有三个方法,这里只重写了一个

        String url = host+"/"+group+"/"+ROUTE_PREFIX+"/"+account+"/order";
        Long ts = utc_timestamp();
        // 设置post请求参数
        HashMap<String, Object> params = new HashMap<>();
        params.put("id", "");
        params.put("orderId",order_id);
        params.put("time",ts);
        params.put("symbol",symbol);
        params.put("respInst",resp_inst);

        Map<String, String> headers = make_auth_headers(ts, "order", apikey, secret);


        return  doPost("DELETE",url, headers, params);
    }

    @Override
    public JSONObject order_query( String order_id) {

        Long ts = utc_timestamp();
        String url = host+"/"+group+"/"+ROUTE_PREFIX+"/cash/order/status/"+order_id;
        Map<String, String> headers = make_auth_headers(ts, "order/status", apikey, secret);
        HashMap<String, String> params = new HashMap<>();
        params.put("orderId",order_id);
        return doGet(url,headers,params);
    }

}
